<?php
namespace home\Model;
use Think\Model;
class ContentModel extends Model
{
    public function parse_content($data,$user_id=""){
        $data['time_str'] =  friendlyDate($data['create_time']);


        if ($data['school_end_time'] > 0){
            $data['work_year'] = intval((time() - $data['school_end_time'])/86400/365);
        }else{
            $data['work_year'] = 0;
        }
        if ($data['city_name'] == null){
            $data['city_name'] = "未填写城市";
        }

        if ($data['major'] == null){
            $data['major'] = "";
        }



        $data['videos'] = json_decode($data['videos'],true);
        $data['imgs'] = json_decode($data['imgs'],true);
        $data['files'] = json_decode($data['files'],true);

        $data['commend_count'] = M("commend")->where('content_id = '.$data['id'])->count();

        $data['lv'] = D('User')->get_lv($data['exp'])['lv'];
        unset($data['exp']);
        if ($user_id != ""){
            $data['is_collection_user'] =
                M('collection')->where('user_id = '.$user_id.' and object_id = '.$data['user_id'].' and type = 2')
                ->count();
            $data['is_collection_content'] =
                M('collection')->where('user_id = '.$user_id.' and object_id = '.$data['id'].' and type = 3')
                    ->count();
            $data['is_commend'] =
                M('commend')->where('user_id = '.$user_id.' and content_id = '.$data['id'])
                    ->count();
            if ($user_id == $data['user_id']) {
                $data['is_me'] = 1;
            }else{
                $data['is_me'] = 0;
            }
        }else{
            $data['is_collection_user'] = 0;
            $data['is_collection_content'] = 0;
            $data['is_commend'] = 0;
            $data['is_me'] = 0;
        }

        if ($data['room_id'] != 0){
            $room = M('room')->where('id = '.$data['room_id'])->find();
            $data['room'] = $room;
        }

        if ($data['class_id'] == 1 && $data['object_id'] != NULL){
            $ask = M('ask')->where('id = '.$data['object_id'])->find();
            $data['answer_number'] = $ask['answer_number'];
            $data['price'] = intval($ask['price']);
            $data['pay_status'] = $ask['pay_status'];
            $data['end_time_str'] = date("Y-m-d H:i:s",$ask['end_time']);
            $data['end_time_date_time_local'] = date("Y-m-d",$ask['end_time'])."T".date("H:i",$ask['end_time']);
            if ($data['pay_status'] == 1){
                $data['pay_status_str'] = "未支付";
            }else if ($data['pay_status'] == 2){
                $data['pay_status_str'] = "正常";
            }else if ($data['pay_status'] == 3){
                $data['pay_status_str'] = "已结算";
            }
        }
        if ($data['class_id'] == 1){
            $data['answers'] = M('content as a')
                ->join('left join user as b on a.user_id = b.id left join city as c on b.city_id = c.id')
                ->field('a.* , b.nick_name ,b.exp, b.head_ico , c.name as city_name , b.major ,b.school_end_time')
                ->where('a.status = 1 and class_id = 2 and ask_id = '.$data['id'])
                ->order('create_time desc')
                ->select();
            $data['answers'] = $this->parse_contents($data['answers'],$user_id);
            $data['answers_count'] = count($data['answers']);
        }
        if ($data['class_id'] == 2 || $data['class_id'] == 3) {
            $data['title'] = "";
        }

        if ($data['class_id'] == 2 || $data['class_id'] == 3 || $data['class_id'] == 6 || $data['class_id'] == 7 || $data['class_id'] == 8 || $data['class_id'] == 9){
            $data['comment'] = M('content as a')
                ->join('left join user as b on a.user_id = b.id left join city as c on b.city_id = c.id left join user as e on a.to_user_id = e.id')
                ->field('a.* ,b.uuid, b.nick_name ,b.exp, b.head_ico , c.name as city_name , b.major ,b.school_end_time ,e.nick_name as to_user_name')
                ->where('a.status = 1 and class_id = 4 and a.fid = '.$data['id'])
                ->order('create_time desc')
                ->select();
            $data['comment'] = $this->parse_contents($data['comment'],$user_id);
            $data['comment_count'] = count($data['comment']);
        }

        if ($data['class_id'] == 9) {
            $fcontent = M('content as a')
                ->join('left join user as b on a.user_id = b.id left join city as c on b.city_id = c.id left join user as e on a.to_user_id = e.id')
                ->field('a.* ,b.uuid ,b.nick_name ,b.exp, b.head_ico , c.name as city_name , b.major ,b.school_end_time ,e.nick_name as to_user_name')
                ->where('a.id = '.$data['fid'])
                ->find();
            //$fcontent = $this->parse_content($fcontent);
            $data['fcontent'] = $this->parse_content($fcontent);
            // $data['share_content'] = $data['content'];
            // $data['content'] = $fcontent['content'];
            // $data['imgs'] = $fcontent['imgs'];
            // $data['videos'] = $fcontent['videos'];
            // $data['files'] = $fcontent['files'];
            $data['from_user_name'] = $fcontent['nick_name'];
            $data['from_user_id'] = $fcontent['uuid'];


        }

        if ($data['class_id'] == 2){

            $data['ask'] = M('content as a')
                ->field('a.*')
                ->where('id = '.$data['ask_id'])
                ->find();
            $data['ask']['answer_count'] = M('content')->where('class_id = 2 and ask_id = '.$data['ask']['id'])->count();
            if ($data['ask']['object_id'] != NULL){
                $ask = M('ask')->where('id = '.$data['ask']['object_id'])->find();
                $data['ask']['answer_number'] = $ask['answer_number'];
                $data['ask']['price'] = intval($ask['price']);

            }
        }


        if ($data['class_id'] == 5 && $data['object_id'] != NULL){
            $work = M('work as a')
                ->join('left join city as b on a.city_id = b.id left join type as c on a.type_id = c.id')
                ->field('a.* , b.name as city_name , c.name as type_name')
                ->where('a.id = '.$data['object_id'])
                ->find();
            $work['end_time_str'] = date('Y-m-d', $work['end_time']);
            $work['end_time_date_time_local'] = date("Y-m-d",$work['end_time'])."T".date("H:i",$work['end_time']);
            $data['work'] = $work;
        }
        return $data;
    }

    public function parse_contents($data,$user_id=""){

        foreach ($data as $key => $value) {
            $data[$key] = $this->parse_content($data[$key],$user_id);
        }

        return $data;
    }
}